package com.oxy.svc.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import com.oxy.action.util.I18nUtil;
import com.oxy.svc.StaticSvc;
import com.oxy.util.Oxy;


public class StaticSvcImpl extends BaseSvc implements StaticSvc {
	private final static Log logger = Oxy.getLogManager().getLogCommon();
	
//	private static String imgDir = SysCfgLoader.getProperty(SysConstants.FSPATH.FSPATH_STATIC_IMG);
	private static String cssDir = Oxy.getCfgManager().App().FSPATH_STATIC_CSS;
	private static String jsDir = Oxy.getCfgManager().App().FSPATH_STATIC_JS;
	
	public String getCssContent(String srcs){
		StringBuffer sb = new StringBuffer();
		if(srcs!=null && !srcs.equals("")){
			String[] srcList = srcs.split(",");
			for(String elem: srcList){
				try {
					File f=new File(cssDir+"/"+elem.trim());
					FileReader fr = new FileReader(f);
					BufferedReader br = new BufferedReader(fr);
					String line = null;
					while((line=br.readLine())!=null){
						sb.append(line.trim());
					}
				} catch (FileNotFoundException e) {
					logger.warn(e.getMessage());
				} catch (IOException e) {
					logger.warn(e.getMessage());
				}
			}
		}
		return decodeParams(sb.toString());
	}
	
	public String getJsContent(String srcs){
		StringBuffer sb = new StringBuffer();
		if(srcs!=null && !srcs.equals("")){
			String[] srcList = srcs.split(",");
			for(String elem: srcList){
				try {
					File f=new File(jsDir+"/"+elem.trim());
					FileReader fr = new FileReader(f);
					BufferedReader br = new BufferedReader(fr);
					String line = null;
					while((line=br.readLine())!=null){
						sb.append(line);
						sb.append("\n");
					}
				} catch (FileNotFoundException e) {
					logger.error(e.getMessage());
				} catch (IOException e) {
					logger.error(e.getMessage());
				}
			}
		}
		return decodeParams(sb.toString());
	}
	
	private String decodeParams(String origStr){
		String retStr = origStr;
		String regx = "\\$\\{(\\s*.+?\\s*)\\}";
		Pattern patt = Pattern.compile(regx);
		Matcher match = patt.matcher(origStr);
		while(match.find()){
			String gotYa = match.group(1);
			String toBeReplaced = "\\$\\{"+gotYa+"\\}";
			String paraExp = gotYa.trim();
			String[] arr = paraExp.split(" ");
			String cmd = arr[0].trim();
			if(cmd.equalsIgnoreCase("message")){
				if(arr.length==2){
					retStr = retStr.replaceFirst(toBeReplaced, I18nUtil.getString(arr[1]));
				}
				else if(arr.length==3){
					retStr = retStr.replaceFirst(toBeReplaced, I18nUtil.getString(arr[1], arr[2]));
				}
			}
			else if(cmd.equalsIgnoreCase("rooturl")){
				retStr = retStr.replaceFirst(toBeReplaced, Oxy.getRootUrl());
			}
			else if(cmd.equalsIgnoreCase("imgpath")){
				retStr = retStr.replaceFirst(toBeReplaced, Oxy.getCfgManager().App().URL_STATIC_IMG);
			}
		}
		return retStr;
	}
	
	public static void main(String args[]){
		StaticSvcImpl test = new StaticSvcImpl();
//		String str="${message label.header.welcome} xx ${rooturl} oo ${ message label.user.greet.login } and ${rootURL} or ${ IMGPath}";
//		String str="$(\"#register #vname\").text(\"${message member.register.namecantbenull}\");";
//		System.out.println(test.decodeParams(str));
		System.out.println(test.getJsContent("test.js"));
	}
}
